﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Avatar.Model;
using AvatarHelper;

namespace Avatar.ResumeService
{
    public static class TrainingExperienceManager
    {
        public static void TrainingExperienceSave(Guid _id,
            string _user,
            string _course,
            string _certificate,
            DateTime _beginDate,
            DateTime _overDate,
            string _institutions,
            int _areaID,
            string _description)
        {
            try
            {
                #region 入口检测

                if (string.IsNullOrEmpty(_user)) throw new Exception("用户名不能为空");
                if (string.IsNullOrEmpty(_course)) throw new Exception("课程名称不能为空");
                if (_user.Length > 50) throw new Exception("用户名不能超过50个字");
                if (_course.Length > 50) throw new Exception("培训课程名称不能超过50个字");
                if (!string.IsNullOrEmpty(_institutions)&&_institutions.Length > 50) throw new Exception("培训机构不能超过50个字");
                if (!string.IsNullOrEmpty(_certificate) && _certificate.Length > 50) throw new Exception("证书名称不能超过50个字");
                if (!string.IsNullOrEmpty(_description) && _description.Length > 500) throw new Exception("说明文字不能超过500个");

                #endregion

                using (var _adc = DBHelper.GetDataContext())
                {
                    var _resume = _adc.Resume.Where(r => r.User == _user).FirstOrDefault();
                    if (_resume != null)
                    {
                        var _t = _resume.TrainingExperience.Where(t => t.ID == _id).FirstOrDefault();
                        if (_t == null)
                        {
                            if (_resume.TrainingExperience.Count() > 10) throw new Exception("培训经历记录每人最多可发10条");
                            _t = new TrainingExperience();
                            _t.ID = Guid.NewGuid();
                            _t.Certificate = _certificate;
                            _t.Course = _course;
                            _t.BeginDate = _beginDate;
                            _t.OverDate = _overDate;
                            _t.Institutions = _institutions;
                            _t.Location = _areaID;
                            _t.Description = _description;
                            _t.User = _user;

                            _adc.TrainingExperience.InsertOnSubmit(_t);
                        }
                        else
                        {
                            _t.Certificate = _certificate;
                            _t.Course = _course;
                            _t.BeginDate = _beginDate;
                            _t.OverDate = _overDate;
                            _t.Institutions = _institutions;
                            _t.Location = _areaID;
                            _t.Description = _description;
                        }
                        _resume.UpdateDate = DateTime.Now;
                        _adc.SubmitChanges();
                    }
                }
            }
            catch (Exception _e)
            {
                throw new Exception("保存培训记录出错：" + _e.Message);
            }
        }
        /// <summary>
        /// 获取培训信息
        /// </summary>
        /// <param name="_id"></param>
        /// <returns></returns>
        public static TrainingExperience GetTrainingExperience(Guid _id)
        {
            return DBHelper.Get<TrainingExperience>(t => t.ID == _id, "获取培训经历出错：");
        }
        /// <summary>
        /// 删除培训信息
        /// </summary>
        /// <param name="_id"></param>
        /// <returns></returns>
        public static bool Delete(Guid _id)
        {
            var _t = GetTrainingExperience(_id);
            return DBHelper.Delete(_t,"删除培训经历失败:");
        }
        /// <summary>
        /// 获取培训信息列表
        /// </summary>
        /// <param name="_user"></param>
        /// <returns></returns>
        public static List<TrainingExperience> GetTrainingExperience(string _user)
        {
            if (string.IsNullOrEmpty(_user)) throw new Exception();

            return DBHelper.GetAll<TrainingExperience>(e=>e.User==_user,"获取培训信息列表出错:");
        }
    }
}
